<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>canvas</title>
  <style>
    body {
      padding-left: 100px;
    }

    canvas {
      box-shadow: 5px 5px 10px #CCC;
    }
  </style>
</head>

<body>
  <p>要使用的视频:</p>
  <video id="video1" controls width="270" autoplay>
    <source src="./images/movie.mp4" type='video/mp4'>
  </video>
  <p>画布 (代码在每20毫秒绘制当前的视频帧):</p>
  <canvas id="canvas" width="500" height="500">
    你的浏览器太low了~
  </canvas>
  <script>

    window.onload = function () {
      var v = document.getElementById("video1");
      var canvas = document.getElementById('canvas')

      if (canvas.getContext) {
        var ctx = canvas.getContext('2d');
        var i = null;
        v.addEventListener('play', function () {
          i = window.setInterval(function () {
            ctx.drawImage(v, 50, 50, 260, 125)
          }, 20);
        }, false);

        v.addEventListener('pause', function () {
          window.clearInterval(i);
        }, false);

        v.addEventListener('ended', function () {
          console.log('ended')
          clearInterval(i);
        }, false);

      } else {
        alert('你的浏览器太low了~')
      }
    }
  </script>

</body>

</html>